#ifndef HTTPENGINE_H
#define HTTPENGINE_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "qeventloop.h"
#include "XmlDbParser.h"

class HttpEngine : public QObject
{
    Q_OBJECT

public:
    enum FinishStatus
    {
        NotFinished,
        Suceed,
        TimeoutExpired,
        Failed,
    };

    enum QueryType
    {
        Select = 0,
        Dml,
    };

protected:
    HttpEngine();
    virtual ~HttpEngine();

public:
    static HttpEngine & httpEngine();
    bool sendRequest( const QString & sqlQuery, QueryType queryType, QByteArray & resultData );
    QString errorStr() const;

private slots:
    void replyFinished( QNetworkReply * reply );
    void onTimeout();

private:
    void appendUrlData( QUrl & url, const QString & sqlQuery, QueryType queryType );
    bool waitForHttpCommunicationFinishes();
    bool processSessionStatus( SessionLoggedStatus::Enum sessionStatus, bool & logged );

private:
    QEventLoop m_eventLoop;
    QNetworkAccessManager * m_manager;
    FinishStatus m_finishStatus;
    QByteArray m_outData;
    XmlDbParser m_xmlDbParser;

    QString m_login;
    QString m_password;
    QByteArray m_sessionId;
    bool m_logged;
    QString m_errorStr;
};

#endif

